<?php

class ReportsModule extends fvModule {

    function __construct () {
        $moduleName = strtolower(substr(__CLASS__, 0, -6));
        parent::__construct(fvSite::$fvConfig->get("modules.{$moduleName}.smarty.template"), 
                            fvSite::$fvConfig->get("modules.{$moduleName}.smarty.compile"), 
                            fvSite::$Layoult);
    }

    function showCustomers() {
    	$filter = fvSite::$fvSession->get(fvRequest::getInstance()->getRequestParameter("requestURL")."/filter");
    	if (!is_array($filter)){
    		 return $this->__display('reports_customer.tpl');    	
    	}
    	$sort = fvRequest::getInstance()->getRequestParameter("sort") == 'asc' ? 'desc' : 'asc';
    	$type = in_array(fvRequest::getInstance()->getRequestParameter("type"), array('id', 'title', 'main_page', 'price', 'discount')) ? fvRequest::getInstance()->getRequestParameter("type") : 'id';
    	$this->__assign('sort', $sort);
    	$params = array();
        if (is_array($filter)) {
        	if (!empty($filter['date_from'])) {
                $query .= (($query)?" AND ":'') . "create_date >= ?";
                $params[] = $filter['date_from'];
                $this->__assign('filter_date_from', $filter['date_from']);
            }
        	if (!empty($filter['date_to'])) {
                $query .= (($query)?" AND ":'') . "create_date <= ?";
                $params[] = $filter['date_to'];
                $this->__assign('filter_date_to', $filter['date_to']);
            }
        }
        $Orders = OrderManager::getInstance()->getAll($query, 'create_date ASC', null, $params);
        $this->__assign('Orders', $Orders);
        $this->__assign('Products', ProductsManager::getInstance());
        $result = array();
        foreach ($Orders as $Order){
        	$result[$Order->customer_id]["count"] += 1;
        	$result[$Order->customer_id]["orders"][$Order->getPk()]["date"] = $Order->create_date;
        	$result[$Order->customer_id]["full_name"] = CustomerManager::getInstance()->getByPk($Order->customer_id)->full_name;
        	foreach ($Order->products as $Product){
        		$current_product = ProductsManager::getInstance()->getByPk($Product["product_id"]);
        		$result[$Order->customer_id]["orders"][$Order->getPk()]["total"] += ($current_product->price-$current_product->price*$current_product->discount/100)*$Product["count"];
        		$result[$Order->customer_id]["total"] += ($current_product->price-$current_product->price*$current_product->discount/100)*$Product["count"];
        	}
        }
        $this->__assign('Report', $result);
        return $this->__display('reports_customer.tpl');    
    }
    
	function showProducts() {
    	$filter = fvSite::$fvSession->get(fvRequest::getInstance()->getRequestParameter("requestURL")."/filter");
		if (!is_array($filter)){
    		 return $this->__display('reports_products.tpl');    	
    	}
    	$sort = fvRequest::getInstance()->getRequestParameter("sort") == 'asc' ? 'desc' : 'asc';
    	$type = in_array(fvRequest::getInstance()->getRequestParameter("type"), array('id', 'title', 'main_page', 'price', 'discount')) ? fvRequest::getInstance()->getRequestParameter("type") : 'id';
    	$this->__assign('sort', $sort);
    	$params = array();
        if (is_array($filter)) {
        	if (!empty($filter['date_from'])) {
                $query .= (($query)?" AND ":'') . "create_date >= ?";
                $params[] = $filter['date_from'];
                $this->__assign('filter_date_from', $filter['date_from']);
            }
        	if (!empty($filter['date_to'])) {
                $query .= (($query)?" AND ":'') . "create_date <= ?";
                $params[] = $filter['date_to'];
                $this->__assign('filter_date_to', $filter['date_to']);
            }
        }
        $Orders = OrderManager::getInstance()->getAll($query, 'create_date ASC', null, $params);
        $this->__assign('Orders', $Orders);
        $this->__assign('Products', ProductsManager::getInstance());
        
        $result = array();
        foreach ($Orders as $Order){
        	foreach ($Order->products as $Product){
        		$current_product = ProductsManager::getInstance()->getByPk($Product["product_id"]);
        		$result[$current_product->getPk()]["count"] += $Product["count"];
        		$result[$current_product->getPk()]["title"] = $current_product->title;
        		$result[$current_product->getPk()]["total"] += ($current_product->price-$current_product->price*$current_product->discount/100)*$Product["count"];
        		$result[$current_product->getPk()]["orders"][$Order->getPk()]["date"] = $Order->create_date;
        		$result[$current_product->getPk()]["orders"][$Order->getPk()]["count"] = $Product["count"];
        		$result[$current_product->getPk()]["orders"][$Order->getPk()]["total"] = ($current_product->price-$current_product->price*$current_product->discount/100)*$Product["count"];
        	}
        }
        $this->__assign('Report', $result);
        return $this->__display('reports_products.tpl');    
    }
    
	function showProduct() {
		$filter = fvSite::$fvSession->get(fvRequest::getInstance()->getRequestParameter("requestURL")."/filter");
    	$this->__assign('ProductManager', ProductsManager::getInstance());
		if (!is_array($filter)){
    		 return $this->__display('reports_product.tpl');    	
    	}
    	$sort = fvRequest::getInstance()->getRequestParameter("sort") == 'asc' ? 'desc' : 'asc';
    	$type = in_array(fvRequest::getInstance()->getRequestParameter("type"), array('id', 'title', 'main_page', 'price', 'discount')) ? fvRequest::getInstance()->getRequestParameter("type") : 'id';
    	$this->__assign('sort', $sort);
    	$params = array();
        if (is_array($filter)) {
        	if (!empty($filter['date_from'])) {
                $query .= (($query)?" AND ":'') . "create_date >= ?";
                $params[] = $filter['date_from'];
                $this->__assign('filter_date_from', $filter['date_from']);
            }
        	if (!empty($filter['date_to'])) {
                $query .= (($query)?" AND ":'') . "create_date <= ?";
                $params[] = $filter['date_to'];
                $this->__assign('filter_date_to', $filter['date_to']);
            }
        	if (!empty($filter['product_id'])) {
                $this->__assign('filter_product_id', $filter['product_id']);
            }
        }
        $Orders = OrderManager::getInstance()->getAll($query, 'create_date ASC', null, $params);
        $this->__assign('Orders', $Orders);
        $this->__assign('Products', ProductsManager::getInstance());
        
        $result = array();
        foreach ($Orders as $Order){
        	foreach ($Order->products as $Product){
        		$current_product = ProductsManager::getInstance()->getByPk($Product["product_id"]);
        		if ($current_product->getPk() == $filter["product_id"]){
	        		$result[$current_product->getPk()]["count"] += $Product["count"];
	        		$result[$current_product->getPk()]["title"] = $current_product->title;
	        		$result[$current_product->getPk()]["total"] += ($current_product->price-$current_product->price*$current_product->discount/100)*$Product["count"];
	        		$result[$current_product->getPk()]["orders"][$Order->getPk()]["date"] = $Order->create_date;
	        		$result[$current_product->getPk()]["orders"][$Order->getPk()]["count"] = $Product["count"];
	        		$result[$current_product->getPk()]["orders"][$Order->getPk()]["total"] = ($current_product->price-$current_product->price*$current_product->discount/100)*$Product["count"];
        		}
        	}
        }
        $this->__assign('Report', $result);
        return $this->__display('reports_product.tpl');    
    }
    
    function showDownload() {
    	$filename[] = "report";
    	$filter = fvSite::$fvSession->get("/".fvRequest::getInstance()->getRequestParameter("module")."/".fvRequest::getInstance()->getRequestParameter("type")."//filter");
    	$dattype = 'text/csv';
    	$type = fvRequest::getInstance()->getRequestParameter("type");
    	$filename[] = fvRequest::getInstance()->getRequestParameter("type");
    	$params = array();
        if (is_array($filter)) {
        	if (!empty($filter['date_from'])) {
                $query .= (($query)?" AND ":'') . "create_date >= ?";
                $params[] = $filter['date_from'];
                $filename[] = date("Ymd",strtotime($filter['date_from']));
            }
        	if (!empty($filter['date_to'])) {
                $query .= (($query)?" AND ":'') . "create_date <= ?";
                $params[] = $filter['date_to'];
                $filename[] = date("Ymd",strtotime($filter['date_to']));
            }
        	if (!empty($filter['product_id'])) {
                $filename[] = "product";
                $filename[] = $filter['product_id'];
            }
        }
        $Orders = OrderManager::getInstance()->getAll($query, 'create_date ASC', null, $params);
        
    	$Filename = implode("_", $filename).'.csv';
    	$result = array();
    	switch ($type){
    		case "customers" :{ 
    			foreach ($Orders as $Order){
		        	$result[$Order->customer_id]["count"] += 1;
		        	$result[$Order->customer_id]["orders"][$Order->getPk()]["date"] = $Order->create_date;
		        	$result[$Order->customer_id]["full_name"] = CustomerManager::getInstance()->getByPk($Order->customer_id)->full_name;
		        	foreach ($Order->products as $Product){
		        		$current_product = ProductsManager::getInstance()->getByPk($Product["product_id"]);
		        		$result[$Order->customer_id]["orders"][$Order->getPk()]["total"] += ($current_product->price-$current_product->price*$current_product->discount/100)*$Product["count"];
		        		$result[$Order->customer_id]["total"] += ($current_product->price-$current_product->price*$current_product->discount/100)*$Product["count"];
		        	}
		        }
    		}break;
    		case "product":{
	    		 foreach ($Orders as $Order){
		        	foreach ($Order->products as $Product){
		        		$current_product = ProductsManager::getInstance()->getByPk($Product["product_id"]);
		        		if ($current_product->getPk() == $filter["product_id"]){
			        		$result[$current_product->getPk()]["count"] += $Product["count"];
			        		$result[$current_product->getPk()]["title"] = $current_product->title;
			        		$result[$current_product->getPk()]["total"] += ($current_product->price-$current_product->price*$current_product->discount/100)*$Product["count"];
			        		$result[$current_product->getPk()]["orders"][$Order->getPk()]["date"] = $Order->create_date;
			        		$result[$current_product->getPk()]["orders"][$Order->getPk()]["count"] = $Product["count"];
			        		$result[$current_product->getPk()]["orders"][$Order->getPk()]["total"] = ($current_product->price-$current_product->price*$current_product->discount/100)*$Product["count"];
		        		}
		        	}
	        	}
    		}break;
    		case "products":{
	    		foreach ($Orders as $Order){
		        	foreach ($Order->products as $Product){
		        		$current_product = ProductsManager::getInstance()->getByPk($Product["product_id"]);
		        		$result[$current_product->getPk()]["count"] += $Product["count"];
		        		$result[$current_product->getPk()]["title"] = $current_product->title;
		        		$result[$current_product->getPk()]["total"] += ($current_product->price-$current_product->price*$current_product->discount/100)*$Product["count"];
		        		$result[$current_product->getPk()]["orders"][$Order->getPk()]["date"] = $Order->create_date;
		        		$result[$current_product->getPk()]["orders"][$Order->getPk()]["count"] = $Product["count"];
		        		$result[$current_product->getPk()]["orders"][$Order->getPk()]["total"] = ($current_product->price-$current_product->price*$current_product->discount/100)*$Product["count"];
		        	}
        		}
    		}break;
    	}
        $this->__assign('Report', $result);
    	$datstring = $this->__display("reports_{$type}_csv.tpl");
		header('Content-Type: ' . $dattype);
        header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Content-Disposition: attachment; filename="' . $Filename . '"');
        header('Content-Length: ' . strlen($datstring));
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
		return $datstring;
    }
    
}

?>
